home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2811 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  78 lines

  1. Path: news.gate.net!pslfl2-40
  2. From: bhutto@gate.net (William Hutto)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: cpp question
  5. Date: 23 Jan 1996 10:02:12 GMT
  6. Organization: CyberGate, Inc.
  7. Message-ID: <4e2bn4$2d8c@news.gate.net>
  8. References: <4drm99$j0m@peabody.colorado.edu> <4dtf1f$21r4@news.gate.net> <4e0580$9v5@solutions.solon.com>
  9. NNTP-Posting-Host: pslfl2-40.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4e0580$9v5@solutions.solon.com>,
  13.    seebs@solutions.solon.com (Peter Seebach) spake:
  14. ;In article <4dtf1f$21r4@news.gate.net>, William Hutto <bhutto@gate.net> 
  15. wrote:
  16. ;>In article <4drm99$j0m@peabody.colorado.edu>,
  17. ;>   woodjr@rintintin.Colorado.EDU (WOOD  JAMEY RYAN) spake:
  18. ;>;I have something like this:
  19. ;>;     #define USERLEN 8
  20. ;>;     #define HOSTLEN 15
  21. ;
  22. ;>;     char name[] = "woodjr";
  23. ;>;     char host[] = "really.long.hostname.com";
  24. ;>;     char s[100];
  25. ;
  26. ;>;     sprintf(s, "%.USERLENs@%.HOSTLENs", name, host);
  27. ;
  28. ;>;And I want cpp to parse the sprintf line to:
  29. ;
  30. ;>;     sprintf(s, "%.8s@%.15s", name, host);
  31. ;
  32. ;>;Which it (of course) isn't doing.  Is there some syntax I
  33. ;>;can use to get it to do this?
  34. ;
  35. ;>There shur is:
  36. ;
  37. ;>     sprintf(s, "%.*s@%.*s", USERLEN, name, HOSTLEN, host);
  38. ;
  39. ;>The asterisk in the width.precision fields in the formatting sequence allows 
  40. ;>you to include the size as an integer value in your parameter list.
  41. ;
  42. ;Yes, but that doesn't do what he wants.  How about
  43. ;#define STR(x) RSTR(x)
  44. ;#define RSTR(x) #x
  45. ;
  46. ;    sprintf(s, "%." STR(USERLEN) "s@%." STR(HOSTLEN) "s", name , host);
  47. ;
  48.  
  49. Well, I don't see any difference in the outcome. I didn't think he 
  50. wanted to actually paste the numbers into the string. However, try your 
  51. solution with variables:
  52.  
  53. #define STR(x) RSTR(x)
  54. #define RSTR(x) #x
  55.  
  56. char *name = "bhutto", *host = "gate.net";
  57. unsigned userlen = 8, hostlen = 15;
  58.  
  59.     sprintf(s, "%." STR(userlen) "s@%." STR(hostlen) "s", name , host);
  60.  
  61. which yields:
  62.  
  63.         sprintf(s, "%.userlens@%.hostlens" , name, host);
  64.                     %.u        %.ho
  65.                     ^^^        ^^^^
  66. So if you were to do this you should, of course, cast the pointers to unsigned 
  67. int. :)
  68.  
  69. Bill
  70.  
  71.  
  72.  
  73. William Hutto
  74. bhutto@gate.net
  75. N4YMN
  76.  
  77. "Whatcha got on?...Your mind?"
  78.